package IO;

import java.io.File;
import java.util.Scanner;

public class Demo3 {

    private static void scanDir(File file, String word){
        File[] files = file.listFiles();
        if(files == null){
            return ;
        }
        for(File f : files){
            System.out.println("当前扫描的文件：" + f.getAbsolutePath());
            if(f.isFile()){
                checkDelete(f, word);
            }else{
                scanDir(f, word);
            }
        }
    }

    private static void checkDelete(File file, String word){
        if(!file.getName().contains(word)){
            return ;
        }
        System.out.println("当前文件名：" + file.getAbsolutePath() + "，请确认是否要删除(Y/N)");
        Scanner scanner = new Scanner(System.in);
        String choice = scanner.next();
        if(choice.equals("Y") || choice.equals("y")){
            file.delete();
            System.out.println("删除完毕");
        }else{
            System.out.println("取消删除");
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("输入要扫描目录：");
        String path = in.nextLine();
        File file = new File(path);
        if(!file.isDirectory()){
            System.out.println("扫描路径错误");
            return ;
        }
        System.out.println("输入要删除的文件");
        String word = in.next();
        scanDir(file, word);
    }
}
